package com.zzyy.study.controller;

import com.zzyy.study.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther zzyy
 * @create 2021-11-14 16:05
 */
@RestController
public class OrderController
{
    @Autowired
    private OrderService orderService;

    /**
     * 场景01：外部方法无事务注解，内部方法添加REQUIRED事务传播类型
     *  在OrderService类的submitOrder方法上无事务注解
     *  在ProductService类的updateProductStockCountById方法上添加@Transactional(propagation = Propagation.REQUIRED)
     *
     * @return
     */
    @RequestMapping(value = "/order/test01")
    public String test01()
    {
        System.out.println("----------> come in test01");
        orderService.submitOrder();

        return "test01";
    }

    /**
     * 场景02：外部方法添加REQUIRED事务传播类型，内部方法无事务注解
     *  在OrderService类的submitOrder方法上添加@Transactional(propagation = Propagation.REQUIRED)
     *  在ProductService类的updateProductStockCountById方法上无事务注解
     * @return
     */
    @RequestMapping(value = "/order/test02")
    public String test02()
    {
        System.out.println("----------> come in test02");
        orderService.submitOrder();

        return "test02";
    }

    /**
     * 场景03：外部方法添加REQUIRED事务传播类型，内部方法添加REQUIRED事务传播类型
     *  在OrderService类的submitOrder方法上添加@Transactional(propagation = Propagation.REQUIRED)
     *  在ProductService类的updateProductStockCountById方法上添加@Transactional(propagation = Propagation.REQUIRED)
     * @return
     */
    @RequestMapping(value = "/order/test03")
    public String test03()
    {
        System.out.println("----------> come in test03");
        orderService.submitOrder();

        return "test03";
    }


    /**
     * 场景04：外部方法添加REQUIRED事务传播类型，内部方法添加NOT_SUPPORTED事务传播类型
     *  在OrderService类的submitOrder方法上添加@Transactional(propagation = Propagation.REQUIRED)
     *  在ProductService类的updateProductStockCountById方法上添加@Transactional(propagation = Propagation.NOT_SUPPORTED)
     * @return
     */
    @RequestMapping(value = "/order/test04")
    public String test04()
    {
        System.out.println("----------> come in test04");
        orderService.submitOrder();

        return "test04";
    }

    /**
     * 场景05：外部方法添加REQUIRED事务传播类型，内部方法添加REQUIRES_NEW事务传播类型
     *  在OrderService类的submitOrder方法上添加@Transactional(propagation = Propagation.REQUIRED)
     *  在ProductService类的updateProductStockCountById方法上添加@Transactional(propagation = Propagation.REQUIRES_NEW)
     * @return
     */
    @RequestMapping(value = "/order/test05")
    public String test05()
    {
        System.out.println("----------> come in test05");
        orderService.submitOrder();

        return "test05";
    }


    /**
     * 场景06：外部方法添加REQUIRED事务传播类型，内部方法添加REQUIRES_NEW事务传播类型
     * 并且将异常代码移动到外部方法尾部
     *  在OrderService类的submitOrder方法上添加@Transactional(propagation = Propagation.REQUIRED)
     *  在ProductService类的updateProductStockCountById方法上添加@Transactional(propagation = Propagation.REQUIRES_NEW)
     * @return
     */
    @RequestMapping(value = "/order/test06")
    public String test06()
    {
        System.out.println("----------> come in test06");
        orderService.submitOrder();

        return "test06";
    }


    /**
     * 场景07：外部方法添加REQUIRED事务传播类型，内部方法添加REQUIRES_NEW事务传播类型
     * 并且将异常代码移动到外部方法尾部
     * 并且内外部方法都在同一个类中
     *  在OrderService类的submitOrder方法上添加@Transactional(propagation = Propagation.REQUIRED)
     *  在ProductService类的updateProductStockCountById方法上添加@Transactional(propagation = Propagation.REQUIRES_NEW)
     * @return
     */
    @RequestMapping(value = "/order/test07")
    public String test07()
    {
        System.out.println("----------> come in test07");
        orderService.submitOrder();

        return "test07";
    }
}
